<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' ); 

function makeSpaces($number) {
	$number = (string)$number;
	$length = strlen($number);
	
	$count = (int)($length / 3);
	if ($count > 0) {
		$new_num = "";
		$i = 0;
		$str = strrev($number);
		while ($i < $count)
			$new_num .= substr($str,($i++)*3,3)." ";
		$new_num .= substr($str,$count*3,$length - $count*3);
		
		return strrev(trim($new_num));
	}
	return $number;
}

$db =& JFactory::getDBO();
$user =& JFactory::getUser();

$db->setQuery("select * from `wed_tools` where `user_id` = '".$user->id."' order by `section`,`wt_id`");
$wed_tools = $db->loadObjectList();
if (!$wed_tools) {
	$db->setQuery("select * from `wed_tools_st` order by `section`,`priority` desc");
	$wed_tools = $db->loadObjectList();
	
	$emptyColumns = array(
		'name' => '',
		'price' => '',
		'count' => '',
		'section' => 'more',
		'priority' => '',
		'fixed' => '',
	);
	$emptyColumns = (object)$emptyColumns;
	array_push($wed_tools,$emptyColumns,$emptyColumns,$emptyColumns);
}
if ($wed_tools) {

	?><link rel="stylesheet" href="<?php echo JRoute::_('/modules/mod_wed_calc/style.css'); ?>" type="text/css" />
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
	<script type="text/javascript" src="<?php echo JRoute::_('/modules/mod_wed_calc/jquery-fieldselection.js'); ?>"></script>
	<script type="text/javascript" src="<?php echo JRoute::_('/modules/mod_wed_calc/wed_calc.js'); ?>"></script>
	<table id="calc_table" border='1' cellspacing="0">
		<tr id="calc_header">
			<td>Наименование</td>
			<td width='80'>Количество</td>
			<td width='120'>Цена</td>
			<td width='100'>Сумма</td>
		</tr>
		<?php
		$sections = array(
			'bride_accessories' => 'Аксессуары для невесты',
			'groom_accessories' => 'Аксессуары для жениха',
			'hall' => 'Банкетный зал',
			'more' => 'Прочие расходы',
		);
		
		$current_section = null;
		
		foreach ($wed_tools as $tool) {
			if ($current_section != $tool->section) {
				echo "<tr class='section' id='{$tool->section}'>";
					echo "<td colspan='3' class='section_switch_off'><span class='switcher'>–</span> {$sections[$tool->section]}</td>";
					echo "<td class='section_summ'></td>";
				echo "</tr>";
				$current_section = $tool->section;
			}
			echo "<tr class='calculate'>";
				echo "<td><input class='calc_input_name" . 
					((!isset($tool->fixed) || $tool->fixed == "fixed") ? " fixed" : "") . "' " .
					((!isset($tool->fixed) || $tool->fixed == "fixed") ? " disabled='disabled'" : "") .
					" type='text' value='".$tool->name."' /></td>";
				echo "<td><input class='calc_input_count' type='text' value='".$tool->count."' /></td>";
				echo "<td><input class='calc_input_price' type='text' value='".makeSpaces($tool->price)."' /></td>";
				echo "<td class='calc_input_sum'></td>";
			echo "</tr>";
		}
			
			
		?><tr id="calc_total_tr">
			<td colspan='3'>Всего расходов:</td>
			<td id="calc_total"></td>
		</tr>
		<tr id="calc_save_tr">
			<td colspan='3' align='right' id="save_result"></td>
			<td><input type='button' id="calc_save" value='Сохранить' /></td>
		</tr>
	</table><?php
}
?>